<?php



          function getpages($link,$table,$cur_page=1,$per_page=2,$page_num=5){

             //求数据表总记录数(as给别名的意思)
             $sql = "select count(*) as total from `$table` ";

             //  执行
              $result = mysqli_query($link,$sql);

              $total_num = mysqli_fetch_assoc($result);



            //求总页码数并向上取整
              $total_page =ceil($total_num['total']/$per_page);

            //求出页码的前后偏移副,(公式=固定页码/2 向下取整)
              $offset_page =floor($page_num/2);

            //计算循环开始值   公式=当前访问的页码-前后偏移副
              $star = $cur_page-$offset_page;

            //计算循环结束值   公式=当前访问的页码+前后偏移副
              $end = $cur_page+$offset_page;

            //判断页码出现负数时
              if($star<1){
                $star=1;
                $end=$page_num;
              }

            // 判断结束值大于总页码数的画则强制将结束值等于总页码数
              if($end>$total_page){
                  $star=$total_page-$page_num+1;
                  $end=$total_page;
              }

               $pages = '';

              //首页优化

                 $disabled = $cur_page == 1?'disabled':'';
                 $href = $cur_page == 1?'#':'index.php?c='.$GLOBALS['c'] .'&a='.$GLOBALS['a'].'&admin=1&p=1';
                 $pages.='<li class="'.$disabled.'">
                                  <a href='.$href.'"index.php?c=news&a=list&admin=1&p=1" class="paginate_button first" tabindex="0" id="datatable1_first">首页</a>
                             </li>';

              //上一页优化
                 $prev = $cur_page-1;
                 $prev_href = $cur_page == 1?'#':'index.php?c=news&a=list&admin=1&p='.$prev;
                 $pages.='<li class="'.$disabled.'"><a href="'.$prev_href.'" class="paginate_button previous" tabindex="0" id="datatable1_previous">上一页</a>
                              </li>';


            //动态生成中间五页页码
              for($i=$star;$i<=$end;$i++){
                      if($i==$cur_page){
                          $pages.='<li  class="active"><span  tabindex="0">'.$i.'</span></li>';
                            }else{
                          $pages.='<li><a href="index.php?c=news&a=list&admin=1&p='.$i.'" tabindex="0">'.$i.'</a></li>';
                                }
                        }
                $disabled = $cur_page == $total_page?'disabled':'';
                $next = $cur_page+1;
                $next_href = $cur_page ==$total_page?'#':'index.php?c=news&a=list&admin=1&p='.$next;


                //下一页优化
                   $pages.= '<li class="'.$disabled.'"><a href="'.$next_href.'" class="paginate_button next" tabindex="0" id="datatable1_next">下一页</a></li>';

                //尾页优化
                   $last_href= $cur_page==$total_page?'#':'index.php?c=news&a=list&admin=1&p='.$total_page;
                   $pages.=' <li class="'.$disabled.'"><a href="'.$last_href.'" class="paginate_button last" tabindex="0" id="datatable1_last">尾页</a></li>';

                       return $pages;

                       }
